﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kiiro.Common.Utility;
using Microsoft.SharePoint;

namespace Kiiro.Common.Entities
{
    [Serializable]
    public class NavigationItem
    {
        public int Id { get; set; }
        public string Title { get; set; }
        public string TargetUrl { get; set; }
        public string SiteLevel { get; set; }
        public int Sequence { get; set; }
        public bool IsVisible { get; set; }
        public List<KiiroGroup> GroupVisibility { get; set; }
        public List<String> PageHiLiteCollection { get; set; }
        public List<String> PageDisplayCollection { get; set; } // currently only used on Sub Nav

        public NavigationItem ( ) { this.GroupVisibility = new List<KiiroGroup> ( ); this.PageHiLiteCollection = new List<string> ( ); this.PageDisplayCollection = new List<string> ( ); }

        /// <summary>
        /// Overloaded Constructor used to Cast SPListItems to NavigationItems in a LINQ expression
        /// </summary>
        /// <param name="navListItem"></param>
        public NavigationItem ( SPListItem navListItem )
        {
            this.MakeNavigationItem ( navListItem );            

            if ( this.PageHiLiteCollection != null )
                this.PageHiLiteCollection.ForEach ( ValidatePageTargetUrls ( ) );

            if ( this.PageDisplayCollection != null )
                this.PageDisplayCollection.ForEach ( ValidatePageTargetUrls ( ) );
        }

        private static Action<string> ValidatePageTargetUrls ( )
        {
            return delegate ( string pageUrl )
            {
                if ( !pageUrl.StartsWith ( "/", StringComparison.OrdinalIgnoreCase ) &&
                    !pageUrl.StartsWith ( "http://", StringComparison.OrdinalIgnoreCase ) &&
                    !pageUrl.StartsWith ( "https://", StringComparison.OrdinalIgnoreCase ) )
                {
                    pageUrl = "/" + pageUrl;
                }
            };
        }
    }
}

/*************************************************************************
 * 
 * $Log: /Internal Projects/Kiiro/Kiiro.Common/Entities/NavigationItem.cs $
 * 
 * 5     1/06/10 12:39p Camerons
 * 
 * 4     21/12/09 5:13p Adamw
 * 
 * 3     21/12/09 2:21p Adamw
 * 
 * 2     21/12/09 2:20p Adamw
 * 
 ************************************************************************/